var express = require('express');
var router = express.Router();
const {uploadFile} = require("../utils/upload");
var {update,getById} = require("../service/moviesService");


router.post('/posterUpload', async function(req, res, next) {
    const {data,success} = await uploadFile(req,{
        fileType:"moviesPoster",
        path:"./public/images"
    })
    if (success) {
        // console.log("上传成功");
        const params = {$push:JSON.stringify({poster:data.url})}
        const result = await update(data._id,params);
        //  await getById(data._id);
        res.send(result);
        return;
    }
});
router.post('/chaUpload', async function(req, res, next) {
    const {data,success} = await uploadFile(req,{
        fileType:"moviesCha",
        path:"./public/images"
    })
    if (success) {
        // console.log("上传成功");
        const params = {$push:JSON.stringify({chaImgs:data.url})}
        const result = await update(data._id,params);
        //  await getById(data._id);
        res.send(result);
        return;
    }
});
router.post('/imgsUpload', async function(req, res, next) {
    const {data,success} = await uploadFile(req,{
        fileType:"moviesImgs",
        path:"./public/images"
    })
    if (success) {
        // console.log("上传成功");
        const params = {$push:JSON.stringify({images:data.url})}
        const result = await update(data._id,params);
        //  await getById(data._id);
        res.send(result);
        return;
    }
});

router.patch('/:_id', async function(req, res, next) {
    const {_id} = req.params;
    const params = req.body;
    const data =  await update(_id,params);
    res.send(data);
});

module.exports = router;